package com.sign2024.sign2024project.goodsmaterials.service.impl;

import com.sign2024.sign2024core.common.constants.CommonConstants;
import com.sign2024.sign2024core.common.exeception.CommonException;
import com.sign2024.sign2024core.common.utils.StringUtils;
import com.sign2024.sign2024core.entry.goodsmaterials.WzHistoricalRelational;
import com.sign2024.sign2024core.entry.goodsmaterials.WzSpecialtySignRelation;
import com.sign2024.sign2024core.entry.goodsmaterials.WzUpdateProject;
import com.sign2024.sign2024core.entry.projecttask.RollbackRecord;
import com.sign2024.sign2024project.goodsmaterials.mapper.WzHistoricalRelationalMapper;
import com.sign2024.sign2024project.goodsmaterials.mapper.WzSpecialtySignRelationMapper;
import com.sign2024.sign2024project.goodsmaterials.mapper.WzUpdateProjectMapper;
import com.sign2024.sign2024project.goodsmaterials.service.WzUpadteDataService;
import com.sign2024.sign2024project.projecttask.mapper.RollbackRecordMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yujie
 * @data 2024/9/12 17:14
 */
@Service
public class WzUpadteDataServiceImpl implements WzUpadteDataService {

    @Resource
    private WzUpdateProjectMapper wzUpdateProjectMapper;

    @Resource
    private WzSpecialtySignRelationMapper wzSpecialtySignRelationMapper;

    @Resource
    private WzHistoricalRelationalMapper wzHistoricalRelationalMapper;

    @Resource
    private RollbackRecordMapper rollbackRecordMapper;

    @Override
    public void addExport(List<WzUpdateProject> projectList, String fileName) {
        if (projectList.isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "excel导入失败");
        }

        // 添加数据
        for (WzUpdateProject wz : projectList) {
            wz.setFileName(fileName);
            wzUpdateProjectMapper.insertWzUpdateProject(wz);
        }
    }

    @Override
    public void saveWzUpdate(Integer taskState) {
        // 判断 数据 是否存在 抽取方案
        List<WzUpdateProject> projectList = wzUpdateProjectMapper.selectWzUpdateProjectAll();
        if (projectList.isEmpty()) {
            throw new CommonException(CommonConstants.ERROR, "请导入抽取方案");
        }

        // 获取历史版本号
        int code = 0;
        Integer versionCode = wzHistoricalRelationalMapper.countMaxVersionCode();
        if (versionCode != null) {
            code = versionCode + 1;
        } else {
            code = code + 1;
        }

        // 获取 表示中 所有数据
        List<WzSpecialtySignRelation> relationList = wzSpecialtySignRelationMapper.selectDataAll();
        // 如果 获取的数据 是 空的 说明 当前数据时 第一版本
        if (!relationList.isEmpty()) {
            for (WzSpecialtySignRelation ss : relationList) {
                WzHistoricalRelational h = new WzHistoricalRelational();
                h.setVersionCode(code);
                h.setSectionName(ss.getSectionName());
                h.setMajorOne(ss.getMajorOne());
                h.setMajorTwo(ss.getMajorTwo());
                h.setMajorThree(ss.getMajorThree());
                h.setMajorFour(ss.getMajorFour());
                h.setMajorFive(ss.getMajorFive());
                wzHistoricalRelationalMapper.insert(h);
            }
        }

        // 更新 数据
        for (WzUpdateProject wz : projectList) {
            // 根据 签报名称 查询 数据
            WzSpecialtySignRelation relation = wzSpecialtySignRelationMapper.selectWzRelation(wz.getSectionName());
            // 如果数据存在 修改
            if (StringUtils.isNotNull(relation)) {
                // 专家专业 从 1 至后 进行挪动  专业 是 1 -5
                WzSpecialtySignRelation ssr = new WzSpecialtySignRelation();
                ssr.setId(relation.getId());
                ssr.setSectionName(wz.getSectionName());
                ssr.setMajorOne(wz.getSignMajor());

                // 1 - 2
                if (StringUtils.isNotNull(relation.getMajorOne())) {
                    ssr.setMajorTwo(relation.getMajorOne());
                }

                // 2 -3
                if (StringUtils.isNotNull(relation.getMajorTwo())) {
                    ssr.setMajorThree(relation.getMajorTwo());
                }

                // 3 - 4
                if (StringUtils.isNotNull(relation.getMajorThree())) {
                    ssr.setMajorFour(relation.getMajorThree());
                }

                // 4 -5
                if (StringUtils.isNotNull(relation.getMajorFour())) {
                    ssr.setMajorFive(relation.getMajorFour());
                }

                wzSpecialtySignRelationMapper.updateWzRelation(ssr);
            } else {
                // 数据 不存在 进行数据的添加
                WzSpecialtySignRelation ssr = new WzSpecialtySignRelation();
                ssr.setSectionName(wz.getSectionName());
                ssr.setMajorOne(wz.getSignMajor());
                wzSpecialtySignRelationMapper.insert(ssr);
            }
        }

        // 添加 回退记录  taskState 任务标识
        RollbackRecord rollback = new RollbackRecord();
        // 文件名
        String projectName = projectList.get(0).getFileName();

        rollback.setFileName(projectName);
        // 任务标识
        rollback.setTaskState(taskState);
        // 数据 版本号
        rollback.setVersionCode(code);
        rollbackRecordMapper.insert(rollback);

        // 数据 更新完成 清空数据
        wzUpdateProjectMapper.cleanWzUpdateProject();
    }
}
